package com.example.service.impl;

import com.example.pojo.Bars;
import com.example.mapper.BarsMapper;
import com.example.pojo.Users;
import com.example.service.IBarsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2024-06-06
 */
@Service
public class BarsServiceImpl extends ServiceImpl<BarsMapper, Bars> implements IBarsService {

    @Resource
    private BarsMapper barsMapper;

    @Resource
    private RedisTemplate<String, List<Bars>> redisTemplate;

    @Override
    public List<Bars> getAllBars() {
        // 首先尝试从 Redis 缓存中获取数据
        String cacheKey = "allBar";
        List<Bars> cachedUser = redisTemplate.opsForValue().get(cacheKey);

        if (cachedUser == null) {
            // 如果缓存中没有数据，则从数据库中获取数据
            List<Bars> barrFromDB = barsMapper.getAllBars(); // 从数据库中获取用户信息

            if (barrFromDB != null) {
                // 将从数据库中获取的数据存入 Redis 缓存
                redisTemplate.opsForValue().set(cacheKey, barrFromDB, Duration.ofMinutes(60)); // 设置缓存时间为60分钟
                return barrFromDB;
            } else {
                // 数据库中没有找到对应用户，返回 null 或者进行其他处理
                return null;
            }
        } else {
            // 如果缓存中有数据，直接返回缓存数据
            return cachedUser;
        }
    }

    @Override
    public List<Users> getUser(String uid) {
        List<Integer> idList = Arrays.stream(uid.split(","))
                .map(Integer::parseInt) // 将分割后的字符串转换为整数
                .collect(Collectors.toList()); // 收集结果到列表
        System.out.println("idList"+idList);
        // 调用Mapper层的方法
        return barsMapper.getUser(idList);
    }

    @Override
    public void StatusChange(int status, int bid) {
        barsMapper.StatusChange(status,bid);
    }

    @Override
    public List<Bars> getFindBars(String select, String input) {
        return barsMapper.getFindBars(select,input);
    }

    @Override
    public List<Bars> FindBar(Integer tid) {
        return barsMapper.FindBar(tid);
    }

    @Override
    public List<Bars> getAllBarsByBid(Integer bid) {
        return barsMapper.getAllBarsByBid(bid);
    }

    @Override
    public List<Bars> userFindBar(Integer uid) {
        return barsMapper.userFindBar(uid);
    }

    @Override
    public List<Bars> getBarsCenter(Integer uid){
        return barsMapper.getBarsCenter(uid);
    }

    @Override
    public List<Bars> getAllApply(Integer bid) {
        return barsMapper.getAllApply(bid);
    }

    @Override
    public void addBars(Integer uid,String bname,Integer tid,String bimage){
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        //格式转化
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String bdata = now.format(formatter);
        barsMapper.addBars(uid,bname,tid,bimage,bdata);
    }

    @Override
    public void updateBarApply(Integer uid, Integer bid,String otherid) {
        barsMapper.updateBarsBybid(bid,otherid);
        barsMapper.updateApply(uid,bid);
    }
}
